package com.wushi.lenovo.asia5b.app_ui.other.dialog

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.Gravity
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import com.mvp.base.BaseDialog
import com.mvp.utils.AppUtil.downloadApp
import com.mvp.utils.AppUtil.isAppInstall
import com.mvp.utils.ResString
import com.mvp.utils.ToastUtil
import com.wushi.lenovo.asia5b.R

/**
 * Created by zhangyuncai on 2020/9/2.
 */
class ChatDialog(context: Context?) : BaseDialog(context) {
    override fun getLayoutId(): Int {
        return R.layout.dialog_chat
    }

    override fun initListener() {
        findViewById<TextView>(R.id.iv_chat_whatsapp).setOnClickListener {
            dismiss()
            openWhatsapp()
        }
        findViewById<TextView>(R.id.iv_chat_messenger).setOnClickListener {
            dismiss()
            openMessager()
        }
    }

    private fun openWhatsapp() {
        val to = "2018"
        val whatsappPackage = "com.whatsapp"
        if (isAppInstall(context, whatsappPackage)) {
            if (TextUtils.equals("2018", to)) {
                //2018 api
                val intent = Intent(Intent.ACTION_VIEW)
                intent.data = Uri.parse("https://api.whatsapp.com/send?phone=60109080255")
                context.startActivity(intent)
            } else {
                //smsto:60109080255
                val uri = Uri.parse("smsto:60109080255") //指定打开phoneNumber发送信息页面
                val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
                sendIntent.setPackage(whatsappPackage) //区别别的应用包名
                context.startActivity(sendIntent)
            }
        } else {
            ToastUtil.showToast(ResString.get(R.string.not_app))
            downloadApp(context, whatsappPackage)
        }
    }

    fun openMessager() {
        val messagerPackage = "com.facebook.orca"
        if (isAppInstall(context, messagerPackage)) {
            val i = Intent(Intent.ACTION_VIEW, Uri.parse("fb-messenger://user/1722485538043321"))
            context.startActivity(i)
        } else {
            val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.messenger.com/t/1722485538043321"))
            context.startActivity(i)
        }
    }

    override fun initView() {
    }
}